package com.cskaoyan.controller.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.promotebean.Coupon;
import com.cskaoyan.bean.promotebean.CouponExample;
import com.cskaoyan.bean.promotebean.CouponUser;
import com.cskaoyan.bean.promotebean.CouponUserExample;
import com.cskaoyan.service.wx.CartService;
import com.cskaoyan.service.wx.CartServiceImpl;
import com.cskaoyan.service.wx.WxCouponService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.wx.CheckOut_FlagVo;
import com.cskaoyan.vo.wx.WxCouponMyListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon/")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;
    @Autowired
    CartServiceImpl cartService;


    public Integer getId() {
        return MyUtils.getUserId();
    }

    /**
     * 用户领取优惠券
     *
     * @param
     * @return
     */
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map) throws ParseException {


        Integer couponId = (Integer) map.get("couponId");

        return wxCouponService.receive(couponId, getId());


    }


    /**
     * 一个复用的方法，根据couponId和userId来添加优惠券，在receive和exchange中使用
     *
     * @param couponId
     * @return
     */
    private CouponUser getCouponUser(Integer couponId) {
        //设置一下参数
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(getId());
        couponUser.setCouponId(couponId);
        Date now = new Date(System.currentTimeMillis());
        couponUser.setAddTime(now);
        couponUser.setUpdateTime(now);
        return couponUser;
    }

    /**
     * 展示已经取得优惠券
     *
     * @param status
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("mylist")
    public BaseRespVo mylist(Short status, Integer page, Integer size) {
        //设置一下必要的参数
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andUserIdEqualTo(getId());
        criteria.andStatusEqualTo(status);
        criteria.andDeletedEqualTo(false);
        if (status == 0){
            couponUserExample.setOrderByClause("add_time desc");

        }
        else {
            couponUserExample.setOrderByClause("update_time desc");
        }
        WxCouponMyListVo wxCouponMyListVo = wxCouponService.mylist(couponUserExample, page, size);
        return BaseRespVo.ok(wxCouponMyListVo);
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map) {
        String code = (String) map.get("code");


        return wxCouponService.exchange(code, getId());
    }


    @RequestMapping("selectlist")
    public BaseRespVo selectList(Integer cartId) {
        //cartId ！= 0 说明是立即购买
        if (cartId != 0) {
            List<Coupon> couponList = wxCouponService.selectList(getId(), cartId);
            return BaseRespVo.ok(couponList);
        }
        //去购物车和其他商品合并购买
        else {


            List<Integer> availableCouponIds = wxCouponService.getAvailableCouponIds(getId(),0);
            List<Coupon> couponList = wxCouponService.getAvailableCoupon(availableCouponIds);
            return BaseRespVo.ok(couponList);


        }
    }
}
